using System;
using System.Collections.Generic;
using System.Text;
using Entities;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
   public  class tbDonHangDAL
    {
       private SqlConnection cn = null;
        public tbDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        
       public void Insert(tbDonHang donhang)
       {
           SqlCommand cm = new SqlCommand("tbDonHang_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaKH", donhang.MaKH);
           cm.Parameters.AddWithValue("@TrangThai", donhang.TrangThai);
           cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
           cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);           
           cm.Parameters.AddWithValue("@HinhThucTT", donhang.HinhThucTT);
           cm.Parameters.AddWithValue("@Diachigiaohang", donhang.Diachigiaohang);
           cm.Parameters.AddWithValue("@Trigia", donhang.Trigia);
           cm.ExecuteNonQuery();
       }
       public void Delete(int ID)
       {
           SqlCommand cm = new SqlCommand("tbDonHang_Detele", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaDH", ID);
           cm.ExecuteNonQuery();

       }
       public void Update(tbDonHang donhang)
       {
           SqlCommand cm = new SqlCommand("tbDonHang_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaDH", donhang.MaDH);
           cm.Parameters.AddWithValue("@MaKH", donhang.MaKH);
           cm.Parameters.AddWithValue("@TrangThai", donhang.TrangThai);
           cm.Parameters.AddWithValue("@NgayDatHang", donhang.NgayDatHang);
           cm.Parameters.AddWithValue("@NgayGiaoHang", donhang.NgayGiaoHang);
           cm.Parameters.AddWithValue("@HinhThucTT", donhang.HinhThucTT);
           cm.Parameters.AddWithValue("@Diachigiaohang", donhang.Diachigiaohang);
           cm.Parameters.AddWithValue("@Trigia", donhang.Trigia);
           cm.ExecuteNonQuery();
       }
       public tbDonHangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbDonHang_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbDonHangCollection donHangCollection = new tbDonHangCollection();
           while (dr.Read())
           {
               tbDonHang Donhang = new tbDonHang();
               Donhang .MaDH  = int.Parse(dr.GetValue(0).ToString());
               Donhang .MaKH = int.Parse(dr.GetValue(1).ToString());               
               Donhang .TrangThai  = int.Parse(dr.GetValue(2).ToString());
               Donhang .NgayDatHang  = DateTime.Parse(dr.GetValue(3).ToString());
               Donhang .NgayGiaoHang  = DateTime.Parse(dr.GetValue(4).ToString());               
               Donhang .HinhThucTT  = int.Parse(dr.GetValue(5).ToString());
               Donhang .Diachigiaohang  = dr.GetValue (6).ToString ();
               Donhang .Trigia  = int.Parse (dr.GetValue (7).ToString ());
               donHangCollection.Add(Donhang);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return donHangCollection;


       }
    }
}
